package com.lcy.web.service.render;

import com.jfinal.core.Controller;
import com.lcy.web.service.base.BaseService;
import com.lcy.web.service.base.MyError;
import com.lcy.web.model.ResponseData;

/**
 * Created by loucunyin on 2017/1/11 0011.
 */
public class JsonRender implements MyRender {
    private BaseService mService;
    public JsonRender(BaseService service) {
        if(mService == null){
            throw new RuntimeException("Service is null");
        }
        mService = service;
    }

    @Override
    public void render( ) {
        ResponseData responseData = new ResponseData();

        MyError error = mService.getError();
        if(error != null){
            responseData.setCode(error.getCode());
            responseData.setMsg(error.getMsg());
        }
        responseData.setData(mService.getData());

        Controller controller = mService.getController();
        if (controller == null){
            throw new RuntimeException("");
        }else {
            mService.getController().renderText(responseData.toJson());
        }
    }

    @Override
    public void renderError() {
        render();
    }
}
